package com.tianma.sync;

import com.tianma.entity.TradeOrders;
import com.tianma.model.BinlogValue;
import com.tianma.service.TradeOrdersService;
import com.tianma.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class TradeOrdersSync extends AbstractSyncTemplate {
    @Autowired
    private TradeOrdersService tradeOrdersService;

    @Override
    void insert(List<Map<String, BinlogValue>> rows) throws Exception {
        saveOrUpdate(rows);
    }

    @Override
    void update(List<Map<String, BinlogValue>> rows) throws Exception {
        saveOrUpdate(rows);
    }

    @Override
    void delete(List<Map<String, BinlogValue>> rows) {
        for (Map<String, BinlogValue> row : rows) {
            BinlogValue id = row.get("order_id");
            tradeOrdersService.removeById(id.getValue());
            log.debug("删除 trade_orders 记录：", id);
        }
    }

    private void saveOrUpdate(List<Map<String, BinlogValue>> rows) throws Exception {
        for (Map<String, BinlogValue> row : rows) {

            Long orderId = Long.parseLong(row.get("order_id").getValue());

            if (orderId < 100000000) continue;

            TradeOrders order = new TradeOrders();
            order.setOrderId(orderId);
            order.setIsComment(StringUtils.isNotEmpty(row.get("is_comment").getValue()) ? Long.parseLong(row.get("is_comment").getValue()) : 0);
            order.setTid(StringUtils.isNotEmpty(row.get("tid").getValue()) ? Long.parseLong(row.get("tid").getValue()) : 0);
            order.setProductId(StringUtils.isNotEmpty(row.get("product_id").getValue()) ? Long.parseLong(row.get("product_id").getValue()) : 0);
            order.setItemId(StringUtils.isNotEmpty(row.get("item_id").getValue()) ? Long.parseLong(row.get("item_id").getValue()) : 0);
            order.setSkuId(StringUtils.isNotEmpty(row.get("sku_id").getValue()) ? Long.parseLong(row.get("sku_id").getValue()) : 0);
            order.setSkuProps(row.get("sku_props").getValue());
            order.setSkuPropsStr(row.get("sku_props_str").getValue());
            order.setGoodsNo(row.get("goods_no").getValue());
            order.setSize1(row.get("size1").getValue());
            order.setSize2(row.get("size2").getValue());
            order.setGoodsType(StringUtils.isNotEmpty(row.get("goods_type").getValue()) ? Integer.parseInt(row.get("goods_type").getValue()) : 0);
            order.setBarcode(row.get("barcode").getValue());
            order.setMarketPrice(StringUtils.isNotEmpty(row.get("market_price").getValue()) ? Double.parseDouble(row.get("market_price").getValue()) : 0);
            order.setPlatPayPrice(StringUtils.isNotEmpty(row.get("plat_pay_price").getValue()) ? Double.parseDouble(row.get("plat_pay_price").getValue()) : 0);
            order.setPrice(StringUtils.isNotEmpty(row.get("price").getValue()) ? Double.parseDouble(row.get("price").getValue()) : 0);
            order.setPayPrice(StringUtils.isNotEmpty(row.get("pay_price").getValue()) ? Double.parseDouble(row.get("pay_price").getValue()) : 0);
            order.setDiscount(StringUtils.isNotEmpty(row.get("discount").getValue()) ? Float.parseFloat(row.get("discount").getValue()) : 0);

            order.setDiscountPrice(StringUtils.isNotEmpty(row.get("discount_price").getValue()) ? Double.parseDouble(row.get("discount_price").getValue()) : 0);
            order.setPostFee(StringUtils.isNotEmpty(row.get("post_fee").getValue()) ? Double.parseDouble(row.get("post_fee").getValue()) : 0);
            order.setCommissionPrice(StringUtils.isNotEmpty(row.get("commission_price").getValue()) ? Double.parseDouble(row.get("commission_price").getValue()) : 0);
            order.setAdjustPrice(StringUtils.isNotEmpty(row.get("adjust_price").getValue()) ? Double.parseDouble(row.get("adjust_price").getValue()) : 0);


            order.setPickingId(StringUtils.isNotEmpty(row.get("picking_id").getValue()) ? Long.parseLong(row.get("picking_id").getValue()) : 0);
            order.setPickingNum(StringUtils.isNotEmpty(row.get("picking_num").getValue()) ? Integer.parseInt(row.get("picking_num").getValue()) : 0);
            order.setMWarehouseId(StringUtils.isNotEmpty(row.get("m_warehouse_id").getValue()) ? Long.parseLong(row.get("m_warehouse_id").getValue()) : 0);
            order.setMWarehouseName(row.get("m_warehouse_name").getValue());
            order.setMDelivery(row.get("m_delivery").getValue());
            order.setOriginalOrderId(StringUtils.isNotEmpty(row.get("original_order_id").getValue()) ? Long.parseLong(row.get("original_order_id").getValue()) : 0);
            order.setStatus(StringUtils.isNotEmpty(row.get("status").getValue()) ? Integer.parseInt(row.get("status").getValue()) : 0);
            order.setOldStatus(StringUtils.isNotEmpty(row.get("old_status").getValue()) ? Integer.parseInt(row.get("old_status").getValue()) : 0);
            order.setWdId(StringUtils.isNotEmpty(row.get("wd_id").getValue()) ? Integer.parseInt(row.get("wd_id").getValue()) : 0);
            order.setRefund(StringUtils.isNotEmpty(row.get("refund").getValue()) ? Float.parseFloat(row.get("refund").getValue()) : 0);
            order.setReturnreason(row.get("returnreason").getValue());

            order.setCheckStatus(StringUtils.isNotEmpty(row.get("check_status").getValue()) ? Integer.parseInt(row.get("check_status").getValue()) : 0);
            order.setCheckDatetime(DateUtils.parseDatetime(row.get("check_datetime").getValue()));
            order.setCheckRemark(row.get("check_remark").getValue());
            order.setIsCompensation(StringUtils.isNotEmpty(row.get("is_compensation").getValue()) ? Integer.parseInt(row.get("is_compensation").getValue()) : 0);
            order.setCompensationRemark(row.get("compensation_remark").getValue());
            order.setCompensationDatetime(DateUtils.parseDatetime(row.get("compensation_datetime").getValue()));
            order.setIsNoShipment(StringUtils.isNotEmpty(row.get("is_no_shipment").getValue()) ? Integer.parseInt(row.get("is_no_shipment").getValue()) : 0);
            order.setNoShipmentRemark(row.get("no_shipment_remark").getValue());
            order.setNoShipmentDatetime(DateUtils.parseDatetime(row.get("no_shipment_datetime").getValue()));
            order.setIsSubmitDeliveryNo(StringUtils.isNotEmpty(row.get("is_submit_delivery_no").getValue()) ? Integer.parseInt(row.get("is_submit_delivery_no").getValue()) : 0);
            order.setSubmitDeliveryNoDatetime(DateUtils.parseDatetime(row.get("submit_delivery_no_datetime").getValue()));
            order.setReturnReason(StringUtils.isNotEmpty(row.get("return_reason").getValue()) ? Integer.parseInt(row.get("return_reason").getValue()) : 0);
            order.setReturnRemark(row.get("return_remark").getValue());
            order.setReturnDatetime(DateUtils.parseDatetime(row.get("return_datetime").getValue()));

            order.setReturnExpNum(row.get("return_exp_num").getValue());
            order.setReturnExpCompany(StringUtils.isNotEmpty(row.get("return_exp_company").getValue()) ? Integer.parseInt(row.get("return_exp_company").getValue()) : 0);
            order.setReturnWhere(StringUtils.isNotEmpty(row.get("return_where").getValue()) ? Integer.parseInt(row.get("return_where").getValue()) : 0);
            order.setFaultPrice(StringUtils.isNotEmpty(row.get("fault_price").getValue()) ? Double.parseDouble(row.get("fault_price").getValue()) : 0);
            order.setFaultOwner(row.get("fault_owner").getValue());
            order.setChangeRemark(row.get("change_remark").getValue());
            order.setChangeDatetime(DateUtils.parseDatetime(row.get("change_datetime").getValue()));
            order.setProcessDays(StringUtils.isNotEmpty(row.get("process_days").getValue()) ? Integer.parseInt(row.get("process_days").getValue()) : 0);
            order.setReturnChangeExportStatus(StringUtils.isNotEmpty(row.get("return_change_export_status").getValue()) ? Integer.parseInt(row.get("return_change_export_status").getValue()) : 0);
            order.setRefundRemark(row.get("refund_remark").getValue());
            order.setRefundDatetime(DateUtils.parseDatetime(row.get("refund_datetime").getValue()));
            order.setReturnDeliveryNo(row.get("return_delivery_no").getValue());
            order.setChangeDeliveryNo(row.get("change_delivery_no").getValue());
            order.setWeight(StringUtils.isNotEmpty(row.get("weight").getValue()) ? Double.parseDouble(row.get("weight").getValue()) : 0);
            order.setNum(StringUtils.isNotEmpty(row.get("num").getValue()) ? Integer.parseInt(row.get("num").getValue()) : 0);
            order.setExportDatetime(DateUtils.parseDatetime(row.get("export_datetime").getValue()));

            order.setIsAccountExtract(StringUtils.isNotEmpty(row.get("is_account_extract").getValue()) ? Integer.parseInt(row.get("is_account_extract").getValue()) : 0);
            order.setIsSupplierBalance(StringUtils.isNotEmpty(row.get("is_supplier_balance").getValue()) ? Integer.parseInt(row.get("is_supplier_balance").getValue()) : 0);
            order.setMkUsername(row.get("mk_username").getValue());
            order.setMkUid(StringUtils.isNotEmpty(row.get("mk_uid").getValue()) ? Long.parseLong(row.get("mk_uid").getValue()) : 0);
            order.setNewAgentOrderId(StringUtils.isNotEmpty(row.get("new_agent_order_id").getValue()) ? Integer.parseInt(row.get("new_agent_order_id").getValue()) : 0);
            order.setOuterOrderId(row.get("outer_order_id").getValue());
            order.setIsReturnExport(StringUtils.isNotEmpty(row.get("is_return_export").getValue()) ? Integer.parseInt(row.get("is_return_export").getValue()) : 0);
            order.setIsReturnedExport(StringUtils.isNotEmpty(row.get("is_returned_export").getValue()) ? Integer.parseInt(row.get("is_returned_export").getValue()) : 0);
            order.setRemarkFlag(StringUtils.isNotEmpty(row.get("remark_flag").getValue()) ? Integer.parseInt(row.get("remark_flag").getValue()) : 0);
            order.setCRemark(row.get("c_remark").getValue());
            order.setWarehouseBigName(row.get("warehouse_big_name").getValue());
            long outOid = StringUtils.isNotEmpty(row.get("out_oid").getValue()) ? Long.parseLong(row.get("out_oid").getValue()) : 0;
            if (outOid > 0) {
                outOid += 14500840;
                order.setOutOid(outOid);
            }
            long outPid = StringUtils.isNotEmpty(row.get("out_pid").getValue()) ? Long.parseLong(row.get("out_pid").getValue()) : 0;
            if (outPid > 0) {
                outPid += 10196057;
                order.setOutPid(outPid);
            }


            log.debug("插入或更新 trade_orders 数据:", order);
            tradeOrdersService.saveOrUpdate(order);
        }
    }


}
